package com.foreveross.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.service.HrFavorableService;
import com.foreveross.service.LogService;
import com.foreveross.util.AuthUtil;
import com.foreveross.vo.HrFavorable;
import com.foreveross.vo.SysUser;
import com.foreveross.vo.common.FileVo;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

@Controller
@RequestMapping(value = "/SysHrFavorableController")
public class SysHrFavorableController{

	private Logger log = Logger.getLogger(SysHrFavorableController.class);

	@Resource
	HrFavorableService hrFavorableService;

	@Resource
	private LogService logService;
	
	/**
	 * 获取润优惠LIST
	 * @param request
	 * @param response
	 * @param currentPage
	 * @param pageSize
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getFavorableList")
	public void getFavorableList(HttpServletRequest request,
			HttpServletResponse response,String userId,String name,String houseId,String businessId,
			String page,String rows) {
		ResultVo resultVo = new ResultVo();
		try {
			
			int total = (int)hrFavorableService.getFavorableListCount(null,userId,name, houseId,businessId);
			PageVo<HrFavorable> pageVo = new PageVo<HrFavorable>(page,total,rows);
			List<HrFavorable> list = hrFavorableService.getFavorableList(null,userId,name, houseId,businessId,
					pageVo.getpageIndex(),pageVo.getpageSize());
			pageVo.setRows(list);
			resultVo.setSuccess(true);
			resultVo.setData(pageVo);
			log.info("润优惠 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("润优惠 查看失败");
			log.error("润优惠 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	/**
	 * 查看润优惠详情
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getFavorableById")
	public void getFavorableById(HttpServletRequest request,
			HttpServletResponse response,String id) {
		ResultVo resultVo = new ResultVo();
		try {
			HrFavorable favorable = hrFavorableService.getFavorableById(id);
			List<FileVo> fileVoList = FileVo.getFileVoList(request, favorable.getImage());
			resultVo.setOther(fileVoList);
			resultVo.setSuccess(true);
			resultVo.setData(favorable);
			log.info("润优惠 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("润优惠  查看失败");
			log.error("润优惠 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 添加更润优惠
	 * 
	 * @param request
	 * @param response
	 * @param HrFavorable
	 * @param updateType
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/addOrUpdateHrFavorable")
	public void addOrUpdateHrFavorable(HttpServletRequest request,
			HttpServletResponse response,String userId, HrFavorable hrFavorable,
			String updateType) {
		ResultVo resultVo = new ResultVo();
		try {
			if(Constants.UPDATE_ADD.equals(updateType)){
				hrFavorable.setCreateId(new SysUser(userId));
				hrFavorable.setCreateTime(new Date());
				hrFavorable.setPublisherId(new SysUser(userId));
			}
			
			hrFavorable.setContentImgList(CommonUtil.getContentImgList(hrFavorable.getContent()));
			
			hrFavorableService.saveOrUpdate(hrFavorable, updateType);
			resultVo.setSuccess(true);
			logService.saveLog(
					CommonUtil.getAddrIp(request),
					Constants.OPERATE_TYPE_BACKGROUND,
					updateType.equals(Constants.UPDATE_ADD) ? Constants.LOG_ADD
							: Constants.LOG_UPDATE,
					"润优惠" + Constants.getUpdateType(updateType),
					AuthUtil.getUserid(request),
					Constants.getUpdateType(updateType) + "润优惠["
							+ hrFavorable.toString() + "]");
			log.info("润优惠 " + updateType + "成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("润优惠 " + updateType + "失败");
			log.error("润优惠" + updateType + "失败!", e);
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	

	/**
	 * 删除润优惠
	 * 
	 * @param request
	 * @param response
	 * @param ids
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/deleteHrFavorable")
	public void deleteHrFavorable(HttpServletRequest request,
			HttpServletResponse response, String ids) {
		ResultVo resultVo = new ResultVo();
		try {
			hrFavorableService.deleteObjecList("HrFavorable", ids);
			resultVo.setSuccess(true);
			logService.saveLog(CommonUtil.getAddrIp(request),
					Constants.OPERATE_TYPE_BACKGROUND, Constants.LOG_DEL,
					"删除惠优选", AuthUtil.getUserid(request), "润优惠ID[" + ids + "]");
			log.info("润优惠删除成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("润优惠删除失败");
			log.error("润优惠删除失败!", e);
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

}
